package net.fenghaitao.domain;

import lombok.Builder;
import lombok.Data;
import net.fenghaitao.utils.ValidationUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：HuangXiong
 * @date ：Created in 2021/1/7 11:45
 * @description：Excel 校验处理结果类
 * @modified By：
 * @version: 1.0
 */
@Data
public class ExcelVerifyHandlerResult {

    /**
     * 是否有错误
     */
    private boolean hasErrors;

    /**
     * 错误信息
     */
    private List<ErrorMessage> errors;


    public ExcelVerifyHandlerResult() {
        this.errors = new ArrayList<>();
    }


    public void addErrorMsg(String ...messages){
        if(CollectionUtils.isEmpty(errors)){
            errors = new ArrayList<>(messages.length);
        }
        for (String msg : messages) {
            errors.add(new ErrorMessage(msg));
        }
    }


    public boolean hasErrors() {
        return hasErrors;
    }

    public void setHasErrors(boolean hasErrors) {
        this.hasErrors = hasErrors;
    }

    /**
     * 获取所有验证信息
     *
     * @return 集合形式
     */
    public List<ErrorMessage> getAllErrors() {
        return errors;
    }

    /**
     * 获取所有验证信息
     *
     * @return 字符串形式
     */
    public String getErrors() {
        return StringUtils.join(errors);
    }

    public void addError(String propertyName, String message) {
        this.errors.add(new ErrorMessage(propertyName, message));
    }

    public void addError(String propertyName, String message,Boolean isSetMsg) {
        this.errors.add(new ErrorMessage(propertyName, message,isSetMsg));
    }

    @Data
    public class ErrorMessage {


        /**
         * 是否主动设置了msg
         */
        private Boolean isSetMsg;

        private String propertyPath;

        private String message;

        public ErrorMessage() {
        }

        public ErrorMessage(String message){
            this.isSetMsg = true;
            this.message = message;
        }

        public ErrorMessage(String propertyPath, String message) {
            this.propertyPath = propertyPath;
            this.message = message;
        }
        public ErrorMessage(String propertyPath, String message,Boolean isSetMsg) {
            this.propertyPath = propertyPath;
            this.message = message;
            this.isSetMsg = isSetMsg;
        }

        @Override
        public String toString() {
            return isSetMsg ?  message : propertyPath + " : " + message;
        }
    }
}



